package com.flyme.module.base.provider.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysExaminationRemoteService;
import com.flyme.module.base.client.entity.SysExamination;
import com.flyme.module.base.provider.service.SysConditionitemService;
import com.flyme.module.base.provider.service.SysExaminationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 考试安排管理
 * @author zyh
 * @date 2020-3-26
 */
@Api(tags = "考试安排")
@RestController
public class SysExaminationController extends BaseController<SysExaminationService, SysExamination> implements SysExaminationRemoteService {

    @Autowired
    private SysConditionitemService conditionitemServiceImpl;

    /**
     * 考试安排列表
     * @return
     */
    @Override
    public ResultBody list(PagerInfo pagerInfo, SysExamination examination) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("*, sysexamType.examTypeName");
        cq.createAlias("sysexamType", "sysexamType");
        if(!examination.getExamType().equals("100")){
            cq.eq("examType", examination.getExamType());
        }
        if(!examination.getExamTypeId().equals(0L)){
            cq.eq("examTypeId", examination.getExamTypeId());
        }
        PqGrid pqGrid = bizService.pageList(cq);
        Map<Long, String> itemMap = conditionitemServiceImpl.selectItems();
        List<EntityMap> lists = (List<EntityMap>) pqGrid.getRows();
        for(EntityMap data : lists){
            String examItemJson = data.get("examItemJson");
            if(ObjectUtils.isEmpty(examItemJson)){
                continue;
            }
            String[] items = examItemJson.split(";");
            String itemName = "";
            for(String it : items){
                String[] typeList = it.split(",");
                for (String type : typeList){
                    itemName += itemMap.get(Long.valueOf(type));
                }
                itemName += "/";
            }
            itemName = itemName.substring(0, itemName.length()-1);
            data.put("examItem", itemName);
        }
        pqGrid.setRows(lists);
        return ResultBody.success(pqGrid);
    }

    @Override
    public ResultBody examinationEdit(SysExamination examination) {
        if(ObjectUtils.isEmpty(examination.getId())){
            Integer e = bizService.count(q().eq("examinationCode", examination.getExaminationCode()));
            if(e > 0){
                return ResultBody.failed("code值不能重复");
            }
            Boolean tag = bizService.save(examination);
            return ResultBody.success("添加成功").setData(tag);
        }
        Boolean tag = bizService.saveOrUpdate(examination);
        return ResultBody.success("更新成功").setData(tag);
    }

    /**
     * 更新课程状态
     */
    @ApiOperation(value = "更新课程状态", notes = "更新课程状态")
    @Override
    public ResultBody setOpenStatus(@RequestParam Long id, @RequestParam Integer isOpen) {
        boolean result = bizService.update(new UpdateWrapper<SysExamination>().set("isOpen",isOpen).eq("id",id));
        return ResultBody.ok("设置成功").setData(result);
    }

    /**
     * 获取活动详情
     * @return
     */
    @ApiOperation(value = "通过id查询")
    @Override
    public ResultBody get(@PathVariable Long id) {
        SysExamination examination = bizService.getById(id);
        System.out.println(JSONUtil.toJsonStr(examination));
        return ResultBody.success(examination);
    }
}
